package ar.com.mooral.activities;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.crohn.view.CrohnButton;
import ar.com.mooral.crohn.view.CrohnCheckBox;
import ar.com.mooral.crohn.view.CrohnImageView;
import ar.com.mooral.data.Depositions;
import ar.com.mooral.data.FoodList;
import ar.com.mooral.data.Medications;
import ar.com.mooral.data.MedicationsList;
import ar.com.mooral.data.SexualActivityIncidence;
import ar.com.mooral.pojo.BasePojo;
import ar.com.mooral.pojo.MedicationPojo;
import ar.com.mooral.pojo.util.PojoComparatorName;


public class Medication extends CrohnMenuActivity {
	
	private Vector<CrohnImageView> deleteImageViews;

	protected void onStart() {
		super.onStart();

        // EDITAR
        ((TextView)findViewById(R.id.lblMedicationEdit)).setOnClickListener(new View.OnClickListener() {
		    public void onClick(View view) {
		    	for (ImageView img : deleteImageViews ) {
		    		img.setVisibility((img.getVisibility()==View.VISIBLE?View.GONE:View.VISIBLE));
		        }
		    }
        });

        
        // AGREGO LOS MEDICAMENTOS
        LinearLayout linearLayoutMedication = (LinearLayout)findViewById(R.id.linearLayoutMedication);
        linearLayoutMedication.removeAllViews();
        
        Medications medications = new Medications(this);
        Vector<Hashtable<String, String>> datas = (Vector<Hashtable<String, String>>)medications.findAll();
        
        Collections.sort(((Vector)datas), new PojoComparatorName());
        deleteImageViews = new Vector<CrohnImageView>();

        for(Hashtable<String,String> data: datas){
        	MedicationPojo pojo = (MedicationPojo)data;
        	
			LinearLayout ll = new LinearLayout(this);
			ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 45));
			ll.setOrientation(LinearLayout.HORIZONTAL);
			ll.setBackgroundResource(R.drawable.medication_bg);
			ll.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
			linearLayoutMedication.addView(ll);			
        	
			
			if (pojo.getComun().equals("0")) {
				CrohnImageView deleteImageView = new CrohnImageView(this);
				deleteImageView.setBackgroundResource(R.drawable.stop);
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(25, 25);
				layoutParams.setMargins(0, 10, 0, 0);
				deleteImageView.setLayoutParams(layoutParams);
				deleteImageView.setVisibility(View.GONE);
				deleteImageView.setPojo(pojo);
				deleteImageView.setOnClickListener(new View.OnClickListener() {
	    		    public void onClick(View view) {
	    		    	BasePojo basePojo = ((CrohnImageView) view).getPojo();
	    		    	basePojo.getDatabaseHelper().delete(basePojo.getId());
	    		    	((LinearLayout) view.getParent().getParent()).removeView((View) view.getParent());
	    		    }
	            });
				deleteImageViews.add(deleteImageView);
				ll.addView(deleteImageView);
			}
			
			//Button recordButton = new Button(this);
			CrohnButton recordButton = new CrohnButton(this);
			String text = pojo.getName();
			if(text.length()>30) text = text.substring(0,30) + "...";
			recordButton.setText(text);
			recordButton.setBackgroundResource(R.drawable.white);
			recordButton.setLayoutParams(new LayoutParams(215, 30));
			//recordButton.setTextColor(Color.BLACK);
			recordButton.setTextSize(11);
			recordButton.setPojo(pojo);
			recordButton.setOnClickListener(new View.OnClickListener() {
    		    public void onClick(View view) {
    		    	BasePojo basePojo = ((CrohnButton) view).getPojo();
		    		CrohnContext.setPojo(basePojo);
    		    	
					Intent intent = new Intent();
					intent.setComponent(new ComponentName(	MedicationAdd.class.getPackage().getName(), 
															MedicationAdd.class.getName()));
					//startActivity(intent);
	         		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
	         		parentActivity.startChildActivity("MedicationAdd", intent);

    		    }
            });
			ll.addView(recordButton);
			
			
			CrohnCheckBox tomado = new CrohnCheckBox(this);
			tomado.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tomado.setPojo(pojo);
			if (pojo.getTomado().equals("1")) {
				tomado.setChecked(true);	
			} else {
				tomado.setChecked(false);
			}
			tomado.setOnClickListener(new View.OnClickListener() {
    		    public void onClick(View view) {

		    		MedicationPojo pojo = (MedicationPojo) ((CrohnCheckBox) view).getPojo();

    		    	if (((CrohnCheckBox) view).isChecked()) {
    		    		pojo.setTomado("1");
    		    	} else {
    		    		pojo.setTomado("0");
    		    	}
    		    	pojo.getDatabaseHelper().save(pojo);
    		    }
            });
			ll.addView(tomado);
			

			
        }

        
		
	}

	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.medication);
        
	    
        
        
		// ADD MEDICATION
        TextView lblMedicationAdd = (TextView) findViewById(R.id.lblMedicationAdd);
        lblMedicationAdd.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(	MedicationAdd.class.getPackage().getName(), 
														MedicationAdd.class.getName()));
				startActivity(intent);
		    }
		});		

    }
}
